package com.example.safeassistan.home.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.RequiresApi;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.safeassistan.R;
import com.example.safeassistan.base.BaseActivity;
import com.example.safeassistan.common.UserData;
import com.example.safeassistan.constants.UrlConstants;
import com.example.safeassistan.hiddendanger.activity.HiddendangerAssess;
import com.example.safeassistan.hiddendanger.activity.HiddendangerCheckAccept;
import com.example.safeassistan.hiddendanger.model.HiddenDangerDetailModel;
import com.example.safeassistan.home.adapter.ImageListAdapter;
import com.example.safeassistan.http.HttpRequest;
import com.example.safeassistan.http.HttpStringCallBack;
import com.example.safeassistan.utile.JsonUtils;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class OverDueHiddenDangerDetail extends BaseActivity implements View.OnClickListener {
    private TextView tvReform;
    private String type="-1";
    private int status=0;
    private String defectId="";//隐患id
    private HiddenDangerDetailModel model=null;

    private TextView tvHiddenDangerTitle,tvRemark,tvSubmitPeo,tvDepartName,tvRiskName
    ,tvControlMeasure,tvExecutor,tvExecuteStatus,tvHiddendDangerLevel,tvReformOverDue,
            tvHiddendDangerType,tvReviewDepart,tvReviewPerson,tvRectTime,tvReviewPerson1,tvReviewTime,
            tvReason,tvDevice,tvReformMesure,tvReformMoney,tvRectTime1,tvAcceptPersonId,tvAcceptTime,
            tvAcceptExplain,tvHiddenReason,tvReviewRemark;
    private LinearLayout llCheckAcceptDetail,llReformDetail,llAssessDetail;
    private RecyclerView rcAcceptImg,rcpic,rcRectImg;//验收附件 隐患附件 整改附件
    private Integer rectSubmit=0;//整改重复提交次数
    private ImageListAdapter adapter;

    private LinearLayout llAssess;

    public static void actionStart(Context context,String type,int status,String defectId,Integer rectSubmit) {
        Intent intent = new Intent(context, OverDueHiddenDangerDetail.class);
        intent.putExtra("type",type);
        intent.putExtra("defectId",defectId);
        intent.putExtra("status",status);
        intent.putExtra("rectSubmit",rectSubmit);
        context.startActivity(intent);
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        setAndroidNativeLightStatusBar(true);
    }

    @Override
    protected int setLayoutResourceID() {
        return R.layout.overdue_hiddendanger_detail;
    }

    @Override
    protected void initView() {
        setTitleText("隐患详情");
        rectSubmit=getIntent().getIntExtra("rectSubmit",0);
        type=getIntent().getStringExtra("type");
        status=getIntent().getIntExtra("status",0);
        defectId=getIntent().getStringExtra("defectId");
        tvExecuteStatus=findViewById(R.id.tvExecuteStatus);
        tvReform=findViewById(R.id.tvReform);
        llAssess=findViewById(R.id.llAssess);
        tvRemark=findViewById(R.id.tvRemark);
        rcRectImg=findViewById(R.id.rcRectImg);
        rcAcceptImg=findViewById(R.id.rcAcceptImg);
        tvHiddenDangerTitle=findViewById(R.id.tvHiddenDangerTitle);
        tvSubmitPeo=findViewById(R.id.tvSubmitPeo);
        tvDepartName=findViewById(R.id.tvDepartName);
        tvRiskName=findViewById(R.id.tvRiskName);
        tvReviewRemark=findViewById(R.id.tvReviewRemark);
        tvControlMeasure=findViewById(R.id.tvControlMeasure);
        tvExecutor=findViewById(R.id.tvExecutor);
        tvHiddendDangerLevel=findViewById(R.id.tvHiddendDangerLevel);
        tvReformOverDue=findViewById(R.id.tvReformOverDue);
        tvHiddendDangerType=findViewById(R.id.tvHiddendDangerType);
        tvReviewDepart=findViewById(R.id.tvReviewDepart);
        tvReviewPerson=findViewById(R.id.tvReviewPerson);
        tvRectTime=findViewById(R.id.tvRectTime);
        tvReviewPerson1=findViewById(R.id.tvReviewPerson1);
        tvReviewTime=findViewById(R.id.tvReviewTime);
        tvReason=findViewById(R.id.tvReason);
        tvDevice=findViewById(R.id.tvDevice);
        tvHiddenReason=findViewById(R.id.tvHiddenReason);
        tvReformMesure=findViewById(R.id.tvReformMesure);
        tvReformMoney=findViewById(R.id.tvReformMoney);
        tvRectTime1=findViewById(R.id.tvRectTime1);
        tvAcceptPersonId=findViewById(R.id.tvAcceptPersonId);
        tvAcceptExplain=findViewById(R.id.tvAcceptExplain);
        tvAcceptTime=findViewById(R.id.tvAcceptTime);
        llCheckAcceptDetail=findViewById(R.id.llCheckAcceptDetail);
        llReformDetail=findViewById(R.id.llReformDetail);
        llAssessDetail=findViewById(R.id.llAssessDetail);
        rcpic=findViewById(R.id.rcpic);

        tvReform.setOnClickListener(this);
        if(status==1){
            tvReform.setVisibility(View.GONE);
        }else {
            tvReform.setVisibility(View.VISIBLE);
        }
        switch (type){
            case "2":
                //待评审
                tvReform.setText("评审");
                llCheckAcceptDetail.setVisibility(View.GONE);
                llReformDetail.setVisibility(View.GONE);
                llAssessDetail.setVisibility(View.GONE);
                break;
            case "3":
                tvReform.setText("整改");
                llReformDetail.setVisibility(View.GONE);
                llCheckAcceptDetail.setVisibility(View.GONE);
                break;
            case "4":
                if (rectSubmit==0){
                    tvReform.setText("验收");
                    llCheckAcceptDetail.setVisibility(View.GONE);
                }else {
                    tvReform.setText("重新整改");
                }
                break;
            default:
                tvReform.setVisibility(View.GONE);
                break;
        }

        if (UserData.create(OverDueHiddenDangerDetail.this).get(UserData.ls_ps).equals("0")){
            llAssess.setVisibility(View.GONE);
            llAssessDetail.setVisibility(View.GONE);
        }else {
            llAssess.setVisibility(View.VISIBLE);
            if (type.equals("2")){
                llAssessDetail.setVisibility(View.GONE);
            }

        }
    }

    @Override
    protected void initData() {
        getDetail();
    }

    @Override
    protected int getTitleBarType() {
        return 0;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tvReform:
                //整改
                switch (type){
                    case "2"://评审隐患
                        HiddendangerAssess.actionStart(getContext(),defectId,model.getResult().getHdangerTitle(),model.getResult().getRiskPointId());
                        break;
                    case "3":
                        HiddendangerReform.actionStart(getContext(),defectId,model.getResult().getHdangerTitle());
                        break;
                    case "4":
                        if (rectSubmit==0){
                            HiddendangerCheckAccept.actionStart(getContext(),defectId,model.getResult().getHdangerTitle(),model.getResult().getHdangerImg());
                        }else {
                            HiddendangerReform.actionStart(getContext(),defectId,model.getResult().getHdangerTitle());
                        }
                        break;
                }
                break;
        }
    }

    public void getDetail(){
        Map jsonObject=new HashMap();
            jsonObject.put("defectId",defectId);
        new HttpRequest(this).doGet(UrlConstants.findDefectById, null,jsonObject, new HttpStringCallBack() {
            @RequiresApi(api = Build.VERSION_CODES.M)
            @Override
            public void onSuccess(Object result) {
                String datas = result.toString();
                JSONObject jsonObject = null;
                try {
                    jsonObject = new JSONObject(datas);
                    String message = jsonObject.getString("message");
                    Boolean success = jsonObject.getBoolean("success");
                    if (success) {
                        Object obj = JsonUtils.fromJson(result.toString(), HiddenDangerDetailModel.class);
                        if (obj instanceof HiddenDangerDetailModel) {
                             model=(HiddenDangerDetailModel) obj;
                            setView();
                      }
                    }else {
                        Toast.makeText(OverDueHiddenDangerDetail.this,message,Toast.LENGTH_SHORT).show();
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(int code, String msg) {
                Log.e("aaaa",msg);
            }
        });
    }

    public void setView(){
        tvHiddenDangerTitle.setText(model.getResult().getHdangerTitle());
        tvRemark.setText(model.getResult().getRemark());
        tvSubmitPeo.setText(model.getResult().getCreateBy());
        tvDepartName.setText(model.getResult().getDepartName());
        tvRiskName.setText(model.getResult().getRiskPointName());
        tvControlMeasure.setText(model.getResult().getMeasure());
        tvExecutor.setText(model.getResult().getRealname());
        tvReviewRemark.setText(model.getResult().getReviewState());//评审说明
        switch (model.getResult().getDisposeStatus()+""){
            case "1":
                tvExecuteStatus.setText("待提交");
                break;
            case "2":
                tvExecuteStatus.setText("待评审");
                break;
            case "3":
                tvExecuteStatus.setText("待整改");
                break;
            case "4":
                tvExecuteStatus.setText("待验收");
                break;
            case "5":
                tvExecuteStatus.setText("已完成");
                break;
            case "6":
                tvExecuteStatus.setText("已撤销");
                break;
        }
        if (model.getResult().getHdangerLevel()==1){
            tvHiddendDangerLevel.setText("一般隐患");
        }else if (model.getResult().getHdangerLevel()==3) {
            tvHiddendDangerLevel.setText("重大隐患");
        }
        tvReformOverDue.setText(model.getResult().getAllottedTime()+"");
        tvHiddendDangerType.setText(model.getResult().getHiddenTypeName());
        tvReviewDepart.setText(model.getResult().getReviewDepartName());
        tvReviewPerson.setText(model.getResult().getReviewPersonName());
        tvReviewPerson1.setText(model.getResult().getReviewPersonName());
        tvRectTime.setText(model.getResult().getRectTime()+"");
        tvReason.setText(model.getResult().getReason());
        tvHiddenReason.setText(model.getResult().getReason());
        tvDevice.setText(model.getResult().getHdangerTitle());
        tvReformMesure.setText(model.getResult().getRectMeasures());
        tvReformMoney.setText(model.getResult().getRectPrice()+"元");
        tvRectTime1.setText(model.getResult().getRectTime()+"");
        tvAcceptPersonId.setText(model.getResult().getAcceptPersonName());
        tvAcceptTime.setText(model.getResult().getAcceptTime());
        tvAcceptExplain.setText(model.getResult().getAcceptExplain());

        if (model.getResult().getAcceptImg()!=null&&model.getResult().getAcceptImg().length()>0){
            List<String> imgs=new ArrayList<>();
            String [] p=model.getResult().getAcceptImg().split(",");
            for (int i=0;i<p.length;i++){
                if (p[i].length()>1&!p[i].equals(",")){
                    imgs.add(p[i]);
                }
            }

            adapter=new ImageListAdapter(imgs);
            rcAcceptImg.setAdapter(adapter);
            rcAcceptImg.setLayoutManager(new GridLayoutManager(this,3,RecyclerView.VERTICAL,false));
        }

        if (model.getResult().getHdangerImg()!=null&&model.getResult().getHdangerImg().length()>0){
            List<String> imgs=new ArrayList<>();
            String [] p=model.getResult().getHdangerImg().split(",");
            for (int i=0;i<p.length;i++){
                if (p[i].length()>0&&!p[i].equals(",")){
                    imgs.add(p[i]);
                }
            }

            adapter=new ImageListAdapter(imgs);
            rcpic.setAdapter(adapter);
            rcpic.setLayoutManager(new GridLayoutManager(this,3,RecyclerView.VERTICAL,false));
        }


        if (model.getResult().getRectImg()!=null&&model.getResult().getRectImg().length()>0){
            List<String> imgs=new ArrayList<>();
            String [] p=model.getResult().getRectImg().split(",");
            for (int i=0;i<p.length;i++){
                if (p[i].length()>0&&!p[i].equals(",")){
                    imgs.add(p[i]);
                }
            }

            adapter=new ImageListAdapter(imgs);
            rcRectImg.setAdapter(adapter);
            rcRectImg.setLayoutManager(new GridLayoutManager(this,3,RecyclerView.VERTICAL,false));
        }
    }
}